package com.zhang.controller;

import com.github.pagehelper.PageInfo;
import com.zhang.pojo.ProductBean;
import com.zhang.pojo.UserBean;
import com.zhang.service.IProductService;
import com.zhang.util.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@RequestMapping("product")
@RestController
public class ProductController {

    @Autowired
    private IProductService productService;

    @RequestMapping("findByItem")
    public PageInfo<ProductBean> findByItem(int pageNo ,String status, String name, LocalDate start, LocalDate end, HttpServletRequest req){
        UserBean user = (UserBean) req.getSession().getAttribute("user");
        if (user == null || user.getGrade().equals("顾客")){
            status = "上架";
        }
        System.out.println("------------------>"+status);
        return productService.findByItem(pageNo, status, name, start, end);
    }

    @RequestMapping("findById")
    public ProductBean findById(int id){
        return productService.findById(id);
    }

    @RequestMapping("add")
    public String add(ProductBean productBean,@RequestParam("file") MultipartFile file){
        String upload = UploadFile.upload(file, "static/html/img");
        productBean.setImgPath(upload);
        productService.add(productBean);
        return "ok";
    }

    @RequestMapping("update")
    public String update(int productId,String status,Integer price){
        productService.update(productId,status,price);
        return "ok";
    }
}
